package com.example.duanjunjie.learn.json;

import android.util.JsonReader;

import com.google.gson.annotations.JsonAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by duanjunjie on 2018/3/20.
 */

@JsonAdapter(GsonTestDataAdapter.class)
public class GsonTestData {
    public int count;
    public List<DataItem> items = new ArrayList<>();

    public void read(com.google.gson.stream.JsonReader reader) throws Exception {
        reader.beginObject();
        while (reader.hasNext()) {
            String nextName = reader.nextName();
            if ("count".equals(nextName)) {
                count = reader.nextInt();
            } else if ("items".equals(nextName)) {
                reader.beginArray();
                while (reader.hasNext()) {
                    DataItem item = new DataItem();
                    item.read(reader);
                    items.add(item);
                }
                reader.endArray();
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
    }

}
